﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class FilmRevenueManagementControl : UserControl
    {
        private CinemaFilmFilmRevenueViewTableAdapter x_CinemaFilmFilmRevenueViewTableAdapter = new CinemaFilmFilmRevenueViewTableAdapter();
        private List<ReportDataSet.CinemaFilmFilmRevenueViewRow> x_ListCinemaFilmFilmRevenue = null;
        ReportDataSet.CinemaFilmFilmRevenueViewRow x_CinemaFilmFilmRevenue = null;
        //
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblFilmTableAdapter x_tblFilmTableAdapter = new tblFilmTableAdapter();
        private List<ReportDataSet.tblFilmRow> x_ListFilm = null;
        //
        private tblFilmRevenueTableAdapter x_tblFilmRevenueTableAdapter = new tblFilmRevenueTableAdapter();
        //
        private void FilmRevenueManagementControl_Load(object sender, EventArgs e)
        {
            initDataGridView();
            //
            gridFilmRevenue.DataSource = ListCinemaFilmFilmRevenue;
            //
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
            //
            cbFilm.DataSource = ListFilm;
            cbFilm.DisplayMember = "FilmTitle";
        }
        public FilmRevenueManagementControl()
        {
            InitializeComponent();
        }
        private void initDataGridView()
        {
            initFilmRevenueDataGridView();
        }
        private void initFilmRevenueDataGridView()
        {
            gridFilmRevenue.AutoGenerateColumns = false;
            gridFilmRevenue.ReadOnly = true;

            DataGridViewTextBoxColumn c01 = new DataGridViewTextBoxColumn();
            c01.DataPropertyName = "FilmRevenueID";
            c01.HeaderText = "Film Revenue ID";

            DataGridViewTextBoxColumn c02 = new DataGridViewTextBoxColumn();
            c02.DataPropertyName = "CinemaName";
            c02.HeaderText = "Cinema Name";

            DataGridViewTextBoxColumn c03 = new DataGridViewTextBoxColumn();
            c03.DataPropertyName = "FilmTitle";
            c03.HeaderText = "Film Title";

            DataGridViewTextBoxColumn c04 = new DataGridViewTextBoxColumn();
            c04.DataPropertyName = "Admission";
            c04.HeaderText = "Admission";

            DataGridViewTextBoxColumn c05 = new DataGridViewTextBoxColumn();
            c05.DataPropertyName = "BO";
            c05.HeaderText = "BO";

            DataGridViewTextBoxColumn c06 = new DataGridViewTextBoxColumn();
            c06.DataPropertyName = "Date";
            c06.HeaderText = "Date";
            //
            gridFilmRevenue.Columns.Add(c01);
            gridFilmRevenue.Columns.Add(c02);
            gridFilmRevenue.Columns.Add(c03);
            gridFilmRevenue.Columns.Add(c04);
            gridFilmRevenue.Columns.Add(c05);
            gridFilmRevenue.Columns.Add(c06);
        }

        private void bindingFilmRevenueData()
        {
            if (CurrentCinemaFilmFilmRevenue != null)
            {
                txtFilmRevenueID.Text = CurrentCinemaFilmFilmRevenue.FilmRevenueID.ToString();
                cbCinema.Text = CurrentCinemaFilmFilmRevenue.CinemaName;
                cbFilm.Text = CurrentCinemaFilmFilmRevenue.FilmTitle;
                dpickerDateCORevenue.Value = CurrentCinemaFilmFilmRevenue.Date;
                txtBO.Text = CurrentCinemaFilmFilmRevenue.BO.ToString();
                txtAdmission.Text = CurrentCinemaFilmFilmRevenue.Admission.ToString();
            }
        }

        public List<ReportDataSet.CinemaFilmFilmRevenueViewRow> ListCinemaFilmFilmRevenue
        {
            get
            {
                if (x_ListCinemaFilmFilmRevenue == null)
                {
                    x_ListCinemaFilmFilmRevenue = x_CinemaFilmFilmRevenueViewTableAdapter.GetData().ToList();
                }
                return x_ListCinemaFilmFilmRevenue;
            }
            set
            {
                x_ListCinemaFilmFilmRevenue = value;
            }
        }

        public ReportDataSet.CinemaFilmFilmRevenueViewRow CurrentCinemaFilmFilmRevenue
        {
            get
            {
                if (x_CinemaFilmFilmRevenue == null)
                {
                    if (gridFilmRevenue.CurrentRow != null)
                    {
                        x_CinemaFilmFilmRevenue = ListCinemaFilmFilmRevenue[gridFilmRevenue.CurrentRow.Index];
                    }
                }
                return x_CinemaFilmFilmRevenue;
            }
            set
            {
                x_CinemaFilmFilmRevenue = value;
            }
        }
        public List<ReportDataSet.tblFilmRow> ListFilm
        {
            get
            {
                if (x_ListFilm == null)
                {
                    x_ListFilm = x_tblFilmTableAdapter.GetData().ToList();
                }
                return x_ListFilm;
            }
            set
            {
                x_ListFilm = value;
            }
        }
        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtBO.Text = "";
            dpickerDateCORevenue.Value = DateTime.Now;
        }

        private void gridFilmRevenue_SelectionChanged(object sender, EventArgs e)
        {
            x_CinemaFilmFilmRevenue = null;
            bindingFilmRevenueData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
                int filmID = ((ReportDataSet.tblFilmRow)cbFilm.SelectedItem).ID;
                int admission = int.Parse(txtAdmission.Text);
                long BOTarget = long.Parse(txtBO.Text);
                DateTime FromDate = dpickerDateCORevenue.Value;
                //
                x_tblFilmRevenueTableAdapter.Insert(CinemaID, filmID, admission, BOTarget, FromDate);

                ListCinemaFilmFilmRevenue = null;
                gridFilmRevenue.DataSource = ListCinemaFilmFilmRevenue;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Insert data fail", "", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int targetID = int.Parse(txtFilmRevenueID.Text);
            //
            ReportDataSet.tblFilmRevenueRow current = x_tblFilmRevenueTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            //
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            int FilmID = ((ReportDataSet.tblFilmRow)cbFilm.SelectedItem).ID;
            long BOTarget = long.Parse(txtBO.Text);
            int admission = int.Parse(txtAdmission.Text);
            DateTime FromDate = dpickerDateCORevenue.Value;
            //
            current.CinemaID = CinemaID;
            current.BO = BOTarget;
            current.Admission = admission;
            current.FilmID = FilmID;
            //
            current.Date = dpickerDateCORevenue.Value;

            x_tblFilmRevenueTableAdapter.Update(current);
            ListCinemaFilmFilmRevenue = null;
            gridFilmRevenue.DataSource = ListCinemaFilmFilmRevenue;
        }
    }
}
